package phase2.rd;

import java.io.File;
import java.util.Scanner;

import phase2.rd.Token.Type;

/**
 * 
 * 
 * @author John Girata
 * @version Dec 8, 2012
 */
public class FileReader {
	public static TokenList getTokens(String filename) throws Exception {
		TokenList tokens = new TokenList();
		Scanner scanner = new Scanner(new File(filename));

		while (scanner.hasNext()) {
			Token token = getToken(scanner.nextLine());

			if (token != null) {
				tokens.add(token);
			}
		}

		return tokens;
	}

	public static Token getToken(String line) {
		int space = line.indexOf(' ');

		String type = line.substring(0, space);
		String value = line.substring(space + 1);

		if (type.charAt(0) == '$') {
			type = type.substring(1);
		}

		return new Token(Type.valueOf(type), value);
	}
}
